<?php

//to show errors
//ini_set('display_errors', 1);

echo "<META HTTP-EQUIV=Refresh CONTENT='60'>";

header("Cache-Control: no-cache");

$gsLIMIT = 1;
$masLIMIT = 1;
$arnLIMIT = 1;
$gsincLIMIT = 1;
$gscitLIMIT= 1;

$gsCol = 0;
$masCol = 0;
$arnCol = 0;
$gsincCol = 0;
$gscitCol = 0;

$xmlFileArray = array("data/all_faculty.xml");

foreach ($xmlFileArray as $xmlFile) {
	if (file_exists($xmlFile)) {

		$xml = simplexml_load_file($xmlFile);

	} else {
		
		echo $xmlFile." File does not exist!";
		exit;
		
	}

	foreach($xml->school as $school) {
		$schoolName = trim($school->attributes()->search);
		$domainName = trim($school->attributes()->domain);
		foreach($school->position as $position) {
			$posName = trim($position->attributes()->name);
			foreach($position->faculty as $faculty) {
				$facultyName = trim($faculty->attributes()->name);
			
				$gsIndex = $faculty->xpath("index[@name='GoogleScholar']");
				$masIndex = $faculty->xpath("index[@name='MicrosoftAcademic']");
				$arnIndex = $faculty->xpath("index[@name='Arnetminer']");
				$gsincIndex = $faculty->xpath("index[@name='GoogleScholarInclusive']");
				$scoIndex = $faculty->xpath("index[@name='Scopus']");
				$gscitIndex = $faculty->xpath("index[@name='GoogleCitation']");
				
				if(!file_exists("data/cap/$schoolName")) {
					mkdir("data/cap/$schoolName");
					mkdir("data/cap/$schoolName/$posName");
					mkdir("data/cap/$schoolName/$posName/$facultyName");
				} else if (!file_exists("data/cap/$schoolName/$posName")) {
					mkdir("data/cap/$schoolName/$posName");
					mkdir("data/cap/$schoolName/$posName/$facultyName");
				} else if (!file_exists("data/cap/$schoolName/$posName/$facultyName")) {
					mkdir("data/cap/$schoolName/$posName/$facultyName");
				}
			
				if(empty($gsIndex) && ($gsCol < $gsLIMIT)) {
					$s = $schoolName;
					$s = str_replace("&","%26",$s);
					$f = $facultyName;
					
					$url = "http://interaction.lille.inria.fr/~roussel/projects/scholarindex/index.cgi?btnG=Search+Scholar&as_sauthors=$f&as_allsubj=all&as_q=\"$s\"&as_oq=&as_eq=&as_publication=&as_ylo=&as_yhi=&x_minr=&as_occt=any&hl=en&lr=";
					
					$doc = new DOMDocument();

					$doc->loadHTMLFile($url);
					
					$text = $doc->saveHTML(); 
					
					$timestamp = date(DATE_W3C);
					
					$path = "data/cap/$schoolName/$posName/$facultyName/gs-current";
					
					mkdir($path);
					
					$doc->saveHTMLFile("$path/$timestamp".".html");
					
					$hindex="";
					$gindex="";
					
					$h_exp = "h-index<\/a>:(.*)\(.*\)<br>"; 	
					$g_exp = "g-index<\/a>:(.*)<\/td>";
					if(preg_match_all("/$h_exp/siU",$text, $h_index_match, PREG_SET_ORDER)) 
					{
						$hindex = trim(htmlspecialchars_decode($h_index_match[0][1]));
						echo "H-Index".$h_index_match[0][1]."<br>";
					}
					if(preg_match_all("/$g_exp/siU",$text, $g_index_match, PREG_SET_ORDER)) 
					{
						$gindex = trim(htmlspecialchars_decode($g_index_match[0][1]));
						echo "G-Index".$g_index_match[0][1]."<br>";

					}
					
					if ((strcmp($hindex,"")==0) || (strcmp($gindex,"")==0)) {
						if ($gsCol > 0) {
							$gsCol += ($gsLIMIT/2);
							echo "Google Scholar: Limit reached, please wait awhile and try again<br />";
						} else {
							$index = $faculty->addChild('index');
							$index->addAttribute('name','GoogleScholar');
							$index->addAttribute('attempt','1');
							$index->addAttribute('timestamp',$timestamp);
						
							echo "google scholar index not collected for: ".$facultyName."; possibly no values to collect<br /><br />";
							$gsCol++;
						}
					} else {
					
						$index = $faculty->addChild('index');
						$index->addAttribute('name','GoogleScholar');
						$index->addAttribute('h',$hindex);
						$index->addAttribute('g',$gindex);
						$index->addAttribute('timestamp',$timestamp);
						
						echo "google scholar index collected for: ".$facultyName."<br /><br />";
						$gsCol++;
					}
				}
				
				if(empty($masIndex) && ($masCol < $masLIMIT)) {
					$s = str_replace(" ","%20",$schoolName);
					$s = str_replace("&","%26",$s);
					$f = str_replace(" ","%20",$facultyName);
					$url="http://academic.research.microsoft.com/Search?query=org%3a%28$s%29%20author%3a%28$f%29&SearchDomain=2";
					
					$doc = new DOMDocument();

					$doc->loadHTMLFile($url);
					
					$text = $doc->saveHTML();
					
					
					$timestamp = date(DATE_W3C);
					
					$path = "data/cap/$schoolName/$posName/$facultyName/mas-list";
					
					mkdir($path);
					
					$doc->saveHTMLFile("$path/$timestamp".".html");

					$hindex="";
					$gindex="";
					
					//echo $text;
					
					//$linkExp ="<a\shref=\"([^\"]*)\">(.*)<\/a>";
					$linkExp ="<li(.*)><a\shref=\"([^\"]*)\">([^<]*<b>[^<]*<\/b>[^<]*)+<\/a>";
					
					if(preg_match_all("/$linkExp/siU", $text, $link_match, PREG_SET_ORDER)) {		
						$link = trim(htmlspecialchars_decode($link_match[0][2]));
						
						$url = "http://academic.research.microsoft.com" . $link;
						
						
						$doc = new DOMDocument();

						$doc->loadHTMLFile($url);
						
						$text = $doc->saveHTML();
						
						$timestamp = date(DATE_W3C);
					
						$path = "data/cap/$schoolName/$posName/$facultyName/mas";
						
						mkdir($path);
						
						$doc->saveHTMLFile("$path/$timestamp".".html");
						
						
						$h_exp = ">H-Index:\s(.*)<\/span>"; 	
						$g_exp = ">G-Index:\s(.*)<\/span>";
						if(preg_match_all("/$h_exp/siU",$text, $h_index_match, PREG_SET_ORDER)) 
						{
							$hindex = trim(htmlspecialchars_decode($h_index_match[0][1]));
							echo "H-Index: ".$h_index_match[0][1]."<br>";
						}
						if(preg_match_all("/$g_exp/siU",$text, $g_index_match, PREG_SET_ORDER)) 
						{
							$gindex = trim(htmlspecialchars_decode($g_index_match[0][1]));
							echo "G-Index: ".$g_index_match[0][1]."<br>";

						}
					} 
					
					if ((strcmp($hindex,"")==0) || (strcmp($gindex,"")==0)) {
						if ($masCol > 0) {
							$masCol += ($masLIMIT/2);
							echo "Microsoft Academic: Limit reached, please wait awhile and try again<br />";
						} else {
							$index = $faculty->addChild('index');
							$index->addAttribute('name','MicrosoftAcademic');
							$index->addAttribute('attempt','1');
							$index->addAttribute('timestamp',$timestamp);
						
							echo "microsoft academic index not collected for: ".$facultyName."; possibly no values to collect<br /><br />";
							$masCol++;
						}
					} else {
					
						$index = $faculty->addChild('index');
						$index->addAttribute('name','MicrosoftAcademic');
						$index->addAttribute('h',$hindex);
						$index->addAttribute('g',$gindex);
						$index->addAttribute('url',$url);
						$index->addAttribute('timestamp',$timestamp);
						
						echo "microsoft academic index collected for: ".$facultyName."<br /><br />";
					
						$masCol++;
					}
					
				}
				
				if(empty($arnIndex) && ($arnCol < $arnLIMIT)) {
					$found = false;
				
					$s = str_replace(" ","%20",$schoolName);
					$s = str_replace("&","%26",$s);
					$f = str_replace(" ","%20",$facultyName);
				
					$url="http://v1.arnetminer.org/expertisesearch.do?searchBtnType=normal&keyword=$f&searchBTN=Search&searchRange=1";
				
					$doc = new DOMDocument();

					$doc->loadHTMLFile($url);
					
					$text = $doc->saveHTML(); 
					
					$timestamp = date(DATE_W3C);
					
					$path = "data/cap/$schoolName/$posName/$facultyName/arnet-disamb";
					
					mkdir($path);
					
					$doc->saveHTMLFile("$path/$timestamp".".html");
					
					//print $text;
					
					//$linkExp = "<div class=\"scroll_area\">(.*)</div>";
					$linkExp = "<div class=\"na_person selected\" title=\"(.*)\" onclick=\"location.href='(.*)'\">";
					
					if(preg_match_all("/$linkExp/siU",$text, $link_match, PREG_SET_ORDER)){
						$title = $link_match[0][1];
						
						//determine if this is the person we are interested in or not.... 
						if(strpos($title, $schoolName)) {
							$link = $link_match[0][2];
							
							//echo $link;
							
							$naid = strpos($link,"naid=");
							$naid = substr($link,$naid+5);
							
							$url = "http://v1.arnetminer.org/viewperson.do?naid=$naid";
							
							$found = true;
							//echo $url;
							
						} else {
							//if not loop through all other options
							$linkExp = "<div class=\"na_person \" title=\"(.*)\" onclick=\"location.href='(.*)'\">";
							
							if(preg_match_all("/$linkExp/siU",$text, $link_match, PREG_SET_ORDER)){
							
								for($i = 0;$i< count($link_match); $i++) {
									$title = $link_match[$i][1];
							
									//determine if this is the person we are interested in or not.... 
									if(strpos($title, $schoolName)) {
										$link = $link_match[$i][2];
										
										//echo $link;
										
										$naid = strpos($link,"naid=");
										$naid = substr($link,$naid+5);
										
										$url = "http://v1.arnetminer.org/viewperson.do?naid=$naid";
										
										$found = true;
										//echo $url;
									}
								}
							}
							
							if($found) {
								
							} else {
								//after loop if we aren't satisfied record issue for later resolution...
								$index = $faculty->addChild('index');
								$index->addAttribute('name','Arnetminer');
								$index->addAttribute('issue','unable to disambiguate');
								$index->addAttribute('timestamp',$timestamp);
							
								echo "arnetminer index not collected for: ".$facultyName."; unable to disambiguate!<br /><br />";
								$arnCol++;
							}
							
							
						}
						
						
						
					} else {
						//no name disambiguation offered, select first author presented
						
						$url="http://v1.arnetminer.org/expertfinding.do?searchBtnType=normal&keyword=$f&searchBTN=Search&searchRange=1";
				
						$doc = new DOMDocument();

						$doc->loadHTMLFile($url);
						
						$text = $doc->saveHTML();
						
						$timestamp = date(DATE_W3C);
					
						$path = "data/cap/$schoolName/$posName/$facultyName/arnet-list";
						
						mkdir($path);
						
						$doc->saveHTMLFile("$path/$timestamp".".html");
						
						//echo $text;
						
						$linkExp = "<a href=\"(.*)\">";
						
						if(preg_match_all("/$linkExp/siU",$text, $link_match, PREG_SET_ORDER)) {
							$link = $link_match[0][1];
							
							//echo $link;
							
							$url = "http://v1.arnetminer.org/$link";
							
							$found = true;
							//echo $url;
							
						} else {
						
							//if no authors presented record attempt
							$index = $faculty->addChild('index');
							$index->addAttribute('name','Arnetminer');
							$index->addAttribute('attempt','1');
							$index->addAttribute('timestamp',$timestamp);
						
							echo "arnetminer index not collected for: ".$facultyName."; possibly no values to collect<br /><br />";
							$arnCol++;
							
							
						}
					}
					
					if($found) {
						//$url hold next link need to follow to get h-index
						$doc = new DOMDocument();

						$doc->loadHTMLFile($url);
						
						$text = $doc->saveHTML(); 
						
						$timestamp = date(DATE_W3C);
					
						$path = "data/cap/$schoolName/$posName/$facultyName/arnet";
						
						mkdir($path);
						
						$doc->saveHTMLFile("$path/$timestamp".".html");
						
						//print $text;
						
						$h_exp = ">H-Index:\s(.*)<\/span>"; 	
						//$g_exp = ">G-Index:\s(.*)<\/span>";
						if(preg_match_all("/$h_exp/siU",$text, $h_index_match, PREG_SET_ORDER)) 
						{
							$hindex = trim(htmlspecialchars_decode($h_index_match[0][1]));
							echo "H-Index: ".$h_index_match[0][1]."<br>";
						}
						/*if(preg_match_all("/$g_exp/siU",$text, $g_index_match, PREG_SET_ORDER)) 
						{
							$gindex = trim(htmlspecialchars_decode($g_index_match[0][1]));
							echo "G-Index: ".$g_index_match[0][1]."<br>";

						}*/
						
						if ((strcmp($hindex,"")==0) /*|| (strcmp($gindex,"")==0)*/) {
							if ($arnCol > 0) {
								$arnCol += ($arnLIMIT/2);
								echo "arnetminer: Limit reached, please wait awhile and try again<br />";
							} else {
								$index = $faculty->addChild('index');
								$index->addAttribute('name','Arnetminer');
								$index->addAttribute('attempt','1');
								$index->addAttribute('timestamp',$timestamp);
							
								echo "arnetminer index not collected for: ".$facultyName."; possibly no values to collect<br /><br />";
								$arnCol++;
							}
						} else {
						
							$index = $faculty->addChild('index');
							$index->addAttribute('name','Arnetminer');
							$index->addAttribute('h',$hindex);
							//$index->addAttribute('g',$gindex);
							$index->addAttribute('url',$url);
							$index->addAttribute('timestamp',$timestamp);
							
							echo "arnetminer index collected for: ".$facultyName."<br /><br />";
						
							$arnCol++;
						}
					}
				}
			
			
				if(empty($gsincIndex) && ($gsincCol < $gsincLIMIT)) {
					$s = $schoolName;
					$s = str_replace("&","%26",$s);
					$f = $facultyName;
					
					$url = "http://interaction.lille.inria.fr/~roussel/projects/scholarindex/index.cgi?btnG=Search+Scholar&as_sauthors=$f&as_allsubj=all&as_q=&as_oq=&as_eq=&as_publication=&as_ylo=&as_yhi=&x_minr=&as_occt=any&hl=en&lr=";
					
					$doc = new DOMDocument();

					$doc->loadHTMLFile($url);
					
					$text = $doc->saveHTML(); 
					
					$timestamp = date(DATE_W3C);
					
					$path = "data/cap/$schoolName/$posName/$facultyName/gs-inclusive";
					
					mkdir($path);
					
					$doc->saveHTMLFile("$path/$timestamp".".html");
					
					$hindex="";
					$gindex="";
					
					$h_exp = "h-index<\/a>:(.*)\(.*\)<br>"; 	
					$g_exp = "g-index<\/a>:(.*)<\/td>";
					if(preg_match_all("/$h_exp/siU",$text, $h_index_match, PREG_SET_ORDER)) 
					{
						$hindex = trim(htmlspecialchars_decode($h_index_match[0][1]));
						echo "H-Index".$h_index_match[0][1]."<br>";
					}
					if(preg_match_all("/$g_exp/siU",$text, $g_index_match, PREG_SET_ORDER)) 
					{
						$gindex = trim(htmlspecialchars_decode($g_index_match[0][1]));
						echo "G-Index".$g_index_match[0][1]."<br>";

					}
					
					if ((strcmp($hindex,"")==0) || (strcmp($gindex,"")==0)) {
						if ($gsincCol > 0) {
							$gsincCol += ($gsincLIMIT/2);
							echo "Google Scholar: Limit reached, please wait awhile and try again<br />";
						} else {
							$index = $faculty->addChild('index');
							$index->addAttribute('name','GoogleScholarInclusive');
							$index->addAttribute('attempt','1');
							$index->addAttribute('timestamp',$timestamp);
						
							echo "google scholar index not collected for: ".$facultyName."; possibly no values to collect<br /><br />";
							$gsincCol++;
						}
					} else {
					
						$index = $faculty->addChild('index');
						$index->addAttribute('name','GoogleScholarInclusive');
						$index->addAttribute('h',$hindex);
						$index->addAttribute('g',$gindex);
						$index->addAttribute('timestamp',$timestamp);
						
						echo "google scholar index collected for: ".$facultyName."<br /><br />";
						$gsincCol++;
					}
				}
				
				if(empty($gscitIndex) && ($gscitCol < $gscitLIMIT)) {
					$found = false;
				
					$s = str_replace(" ","%20",$schoolName);
					$s = str_replace("&","%26",$s);
					$f = str_replace(" ","+",$facultyName);
					
					$url = "http://scholar.google.com/citations?hl=en&view_op=search_authors&mauthors=$f";
					
					$doc = new DOMDocument();

					$doc->loadHTMLFile($url);
					
					$text = $doc->saveHTML(); 
					
					//echo $text;
					
					$gscitCol++;
					
					$timestamp = date(DATE_W3C);
					
					$path = "data/cap/$schoolName/$posName/$facultyName/gs-citation-list";
					
					mkdir($path);
					
					$doc->saveHTMLFile("$path/$timestamp".".html");
					
					
					
					$linkExp = "<a class=\"cit-dark-large-link\" href=\"([^\"]*)\">(.*)Verified email at(.*)<br>";
					
					if(preg_match_all("/$linkExp/siU",$text, $link_match, PREG_SET_ORDER)){
		
						//loop through all options
						
							for($i = 0;($i< count($link_match) && !$found); $i++) {
								$title = $link_match[$i][3];
								
								//print $title;
						
								//determine if this is the person we are interested in or not.... 
								if(strpos($title, $domainName)) {
									$link = $link_match[$i][1];
									
									$url = "http://scholar.google.com".$link;
									
									$found = true;
									//echo $url;
								}
							}
						
						if($found) {
						
							//$url hold next link need to follow to get h-index
							$doc = new DOMDocument();

							$doc->loadHTMLFile($url);
							
							$text = $doc->saveHTML(); 
							
							$timestamp = date(DATE_W3C);
						
							$path = "data/cap/$schoolName/$posName/$facultyName/gs-citation";
							
							mkdir($path);
							
							$doc->saveHTMLFile("$path/$timestamp".".html");
							
							//print $text;
							
							$h_exp = "<td class=\"cit-borderleft cit-data\">(.*)<\/td>"; 	
							//$g_exp = ">G-Index:\s(.*)<\/span>";
							if(preg_match_all("/$h_exp/siU",$text, $h_index_match, PREG_SET_ORDER)) 
							{
								$hindex = trim(htmlspecialchars_decode($h_index_match[2][1]));
								echo "H-Index: ".$h_index_match[2][1]."<br>";
							}
							/*if(preg_match_all("/$g_exp/siU",$text, $g_index_match, PREG_SET_ORDER)) 
							{
								$gindex = trim(htmlspecialchars_decode($g_index_match[0][1]));
								echo "G-Index: ".$g_index_match[0][1]."<br>";

							}*/
							
							if ((strcmp($hindex,"")==0) /*|| (strcmp($gindex,"")==0)*/) {
								if ($gscitCol > 0) {
									$gscitCol += ($gscitLIMIT/2);
									echo "google citation: Limit reached, please wait awhile and try again<br />";
								} else {
									$index = $faculty->addChild('index');
									$index->addAttribute('name','GoogleCitation');
									$index->addAttribute('attempt','1');
									$index->addAttribute('timestamp',$timestamp);
								
									echo "google citation index not collected for: ".$facultyName."; possibly no values to collect<br /><br />";
									$gscitCol++;
								}
								
							}  else {
						
							$index = $faculty->addChild('index');
							$index->addAttribute('name','GoogleCitation');
							$index->addAttribute('h',$hindex);
							//$index->addAttribute('g',$gindex);
							$index->addAttribute('url',$url);
							$index->addAttribute('timestamp',$timestamp);
							
							echo "google citation index collected for: ".$facultyName."<br /><br />";
						
							$gscitCol++;
							}
							
						} else {
							//after loop if we aren't satisfied record issue for later resolution...
							$index = $faculty->addChild('index');
							$index->addAttribute('name','GoogleCitation');
							$index->addAttribute('issue','unable to disambiguate');
							$index->addAttribute('timestamp',$timestamp);
						
							echo "google citation index not collected for: ".$facultyName."; unable to disambiguate!<br /><br />";
							$gscitCol++;
						}
					} else {
					
							//if no authors presented record attempt
							$index = $faculty->addChild('index');
							$index->addAttribute('name','GoogleCitation');
							$index->addAttribute('attempt','1');
							$index->addAttribute('timestamp',$timestamp);
						
							echo "google citation index not collected for: ".$facultyName."; possibly no values to collect<br /><br />";
							$arnCol++;
					}
				}
				

			
			}
		}
	}



$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save($xmlFile);

}

?>